package com.hzy.fivepalace.wx.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzy.fivepalace.common.type.HtmlTypeEnums;
import com.hzy.fivepalace.common.util.DateUtils;
import com.hzy.fivepalace.common.util.ResponseUtil;
import com.hzy.fivepalace.common.util.TableDataInfo;
import com.hzy.fivepalace.db.dao.DtsVideoMapper;
import com.hzy.fivepalace.db.dao.ex.DtsGoodsMapperEx;
import com.hzy.fivepalace.db.dao.ex.DtsHomeIndexMapperEx;
import com.hzy.fivepalace.db.dao.ex.DtsHtmlMapperEx;
import com.hzy.fivepalace.db.domain.*;
import com.hzy.fivepalace.db.service.DtsGroupOpenListService;
import com.hzy.fivepalace.db.service.DtsGroupProductService;
import com.hzy.fivepalace.db.vo.wx.WxGoodsListVo;
import com.hzy.fivepalace.db.vo.wx.WxHomeGoodsIndexVo;
import com.hzy.fivepalace.db.vo.wx.WxHomeSlideshowVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 首页服务
 */
@Service
public class WxHomeService {

    @Resource
    private DtsHomeIndexMapperEx homeIndexMapperEx;
    @Resource
    private DtsVideoMapper videoMapper;
    @Resource
    private DtsGoodsMapperEx goodsMapperEx;
    @Resource
    private DtsHtmlMapperEx htmlMapperEx;
    @Resource
    private DtsGroupProductService groupProductService;
    @Resource
    private DtsGroupOpenListService groupOpenListService;

    @Value("${sys-config.imgUrl}")
    private String imgUrl;

    /**
     * 获取首页模块商品
     *
     * @return
     */
    public Object getHomeModuleGoods() {
        List<WxHomeGoodsIndexVo> list = homeIndexMapperEx.getHomeModuleGoods();
        return ResponseUtil.ok(list);
    }


    /**
     * 获取首页模块商品
     *
     * @return
     */
    public Object getHomeGroupsGoods() {
        QueryWrapper<DtsGroupProduct> qw = new QueryWrapper<>();
        qw.le("begin_time", new Date());
        qw.ge("end_time", new Date());
        qw.eq("state", 1);
        List<DtsGroupProduct> list = groupProductService.list(qw);
        if(CollUtil.isNotEmpty(list)){
            for (DtsGroupProduct product : list){
                if(StrUtil.isNotEmpty(product.getActiveImg())){
                    product.setActiveImgPath(imgUrl + product.getActiveImg());
                }

                product.setIsHasGroup(0);
                QueryWrapper<DtsGroupOpenList> qwOpen = new QueryWrapper<>();
                qwOpen.eq("group_id", product.getId());
                qwOpen.eq("state", 0);
                qwOpen.last("limit 1");
                DtsGroupOpenList groupOpenList = groupOpenListService.getOne(qwOpen);
                if(groupOpenList != null){
                    product.setIsHasGroup(1);
                }
                product.setGroupOpenList(groupOpenList);


                if(product.getBeginTime() != null){
                    product.setWxBeginTime(DateUtils.parseDateToStr("yyyy/MM/dd HH:mm",product.getBeginTime()));
                }
                if(product.getEndTime() != null){
                    product.setWxEndTime(DateUtils.parseDateToStr("yyyy/MM/dd HH:mm",product.getEndTime()));
                }

            }
        }
        return ResponseUtil.ok(list);
    }


    /**
     * 获取首页轮播图
     *
     * @return
     */
    public Object getHomeSlideshow() {
        List<WxHomeSlideshowVo> list = homeIndexMapperEx.getHomeSlideshow();
        return ResponseUtil.ok(list);
    }

    /**
     * 获取首页热门推荐商品列表
     * @param page
     * @param size
     * @return
     */
    public TableDataInfo getHomeRecommendGoodsList(Integer page, Integer size) {
        Page p = PageHelper.startPage(page, size);
        List<WxGoodsListVo> list = goodsMapperEx.getHomeRecommendGoodsList();
        if (CollectionUtils.isEmpty(list)) {
            list = new ArrayList<>();
        } else {
            list.forEach(goods -> {
                goods.setInventory(null == goods.getInventory() ? 0 : goods.getInventory());
                goods.setGoodsPrice(null == goods.getGoodsPrice() ? new BigDecimal("0") : goods.getGoodsPrice());
            });
        }
        return TableDataInfo.getDataTable(list, p.getTotal());
    }

    /**
     * 获取公告列表
     * @return
     */
    public TableDataInfo getNoticeList(){

        List<DtsNoticeWithBLOBs> list=new ArrayList<>();
        String content = htmlMapperEx.getHtmlContentByType(HtmlTypeEnums.TYPE5.getId());
        if(!StringUtils.isBlank(content)){
            DtsNoticeWithBLOBs noticeWithBLOBs=new DtsNoticeWithBLOBs();
            noticeWithBLOBs.setId(1);
            noticeWithBLOBs.setContent(content);
            noticeWithBLOBs.setTitle(content);
            list.add(noticeWithBLOBs);
        }
        return TableDataInfo.getDataTable(list,1);
    }

    /**
     * 品牌视频列表
     *
     * @param page
     * @param size
     * @return
     */
    public TableDataInfo getVideoList(Integer page, Integer size) {
        DtsVideoExample query = new DtsVideoExample();
        query.setOrderByClause("sort asc,add_time desc");
        query.createCriteria().andDeletedEqualTo(false).andRecommendEqualTo(false);
        Page p = PageHelper.startPage(page, size);
        List<DtsVideo> list = videoMapper.selectByExample(query);
        if (CollectionUtils.isEmpty(list)) {
            list = new ArrayList<>();
        }
        return TableDataInfo.getDataTable(list, p.getTotal());
    }

    /**
     * 获取推荐视频
     *
     * @return
     */
    public Object getRecommendVideo() {
        DtsVideoExample query = new DtsVideoExample();
        query.setOrderByClause("add_time desc");
        query.createCriteria().andDeletedEqualTo(false).andRecommendEqualTo(true);
        return ResponseUtil.ok("查询成功", videoMapper.selectOneByExample(query));
    }
}
